// ===============================================
// toast
//
// Create by Will on 2020/10/5 5:57 PM
// Copyright @Will.All rights reserved.
// ===============================================

import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'animation/fade.dart';
import 'core_kit_const.dart';
import 'utils/error_utils.dart';
import 'widget/toast/icon_text.dart';
import 'widget/toast/loading.dart';

const TransitionBuilder Function() CoreKitToastInit = BotToastInit;

class CoreKitToastNavigatorObserver extends BotToastNavigatorObserver {}

class CoreKitToastContainer extends StatelessWidget {
  const CoreKitToastContainer({
    super.key,
    required this.child,
  });

  final Widget child;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: CoreKitConst.toastPadding,
      decoration: const BoxDecoration(
        color: CoreKitConst.toastBackgroundColor,
        borderRadius: CoreKitConst.toastRadius,
      ),
      child: DefaultTextStyle(
        style: CoreKitConst.toastTextStyle,
        textAlign: TextAlign.center,
        overflow: TextOverflow.ellipsis,
        child: child,
      ),
    );
  }
}

class CoreKitToast {
  CoreKitToast();

  static CancelFunc showLoading({
    String? text,
    ValueListenable<String>? textListenable,
    BackButtonBehavior backButtonBehavior = BackButtonBehavior.ignore,
    bool crossPage = false,
    bool clickClose = false,
  }) {
    return BotToast.showCustomLoading(
      toastBuilder: (_) => LoadingWidget(
        text ?? 'Loading',
        textListenable: textListenable,
      ),
      clickClose: clickClose,
      ignoreContentClick: true,
      crossPage: crossPage,
    );
  }

  static CancelFunc showSuccess(
    String text, {
    bool crossPage = false,
    bool clickClose = false,
    VoidCallback? onClose,
    Duration duration = CoreKitConst.toastDuration,
  }) {
    return showIconText(
      // icon: CoreRes.assets_toast_icon_done_png,
      icon: const Icon(
        CupertinoIcons.check_mark_circled,
        size: 30,
        color: Colors.green,
      ),
      text: text,
      crossPage: crossPage,
      clickClose: clickClose,
      onClose: onClose,
      duration: duration,
    );
  }

  static CancelFunc showFailure(
    String? text, {
    bool crossPage = false,
    bool clickClose = false,
  }) {
    return showIconText(
      // icon: CoreRes.assets_toast_icon_error_png,
      icon: const Icon(
        CupertinoIcons.clear_circled,
        size: 30,
        color: Colors.red,
      ),
      text: text,
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showError(
    dynamic error, {
    bool crossPage = false,
    bool clickClose = false,
  }) {
    final errMessage = ErrorUtils.messageFrom(error);
    return showFailure(
      errMessage,
      crossPage: crossPage,
      clickClose: clickClose,
    );
  }

  static CancelFunc showIconText({
    Widget? icon,
    String? text,
    bool crossPage = false,
    bool clickClose = false,
    Color backgroundColor = Colors.transparent,
    Duration duration = CoreKitConst.toastDuration,
    VoidCallback? onClose,
  }) {
    return showWidget(
      IconTextWidget(
        icon: icon,
        text: text,
      ),
      crossPage: crossPage,
      clickClose: clickClose,
      backgroundColor: backgroundColor,
      onClose: onClose,
    );
  }

  static CancelFunc showWidget(
    Widget child, {
    bool crossPage = false,
    bool clickClose = false,
    Color backgroundColor = Colors.transparent,
    Duration duration = CoreKitConst.toastDuration,
    VoidCallback? onClose,
  }) {
    return BotToast.showAnimationWidget(
      toastBuilder: (_) => SafeArea(
        child: Align(
          child: child,
        ),
      ),
      animationDuration: CoreKitConst.toastAnimationDuration,
      wrapToastAnimation: (controller, _, child) {
        return FadeAnimation(
          controller: controller,
          child: child,
        );
      },
      crossPage: crossPage,
      clickClose: clickClose,
      backgroundColor: backgroundColor,
      duration: duration,
      onClose: onClose,
    );
  }
}
